﻿using ChangeMgr;
using System;
using System.Data;
using System.Transactions;

namespace ChangeLogExample
{
    class Program
    {
        static void Main(string[] args)
        {
            TransactionOptions opt = new TransactionOptions();
            opt.IsolationLevel = System.Transactions.IsolationLevel.ReadCommitted;
            opt.Timeout = TimeSpan.FromMinutes(10);
            using (TransactionScope tr = new TransactionScope(TransactionScopeOption.Required, opt))
            {
                string connectionString = "your_connection_string_here"; // 替换为实际的连接字符串

                // 实例化 DatabaseAccess 类
                var dbAccess = new DatabaseAccess(connectionString);

                try
                {
                    dbAccess.OpenConnection();

                    // 获取变动信息
                    var changeLogData = dbAccess.GetChangeLogData();

                    foreach (DataRow row in changeLogData.Rows)
                    {
                        var projectInfo = new ProjectInfo();

                        projectInfo.ProjectId = Convert.ToInt32(row["ProjectId"]);
                        projectInfo.ChangeType = row["ChangeType"].ToString();
                        projectInfo.FieldName = row["FieldName"].ToString();
                        projectInfo.OldValue = row["OldValue"].ToString();
                        projectInfo.NewValue = row["NewValue"].ToString();

                        // 根据字段对照表将科研系统的变动的字段转换为财务系统的字段
                        projectInfo.MappedUserId = dbAccess.MapUserId(projectInfo.OldValue);

                        // 更新财务系统中的数据
                        dbAccess.UpdateFinancialSystemData(projectInfo);

                        Console.WriteLine($"ProjectId: {projectInfo.ProjectId}, ChangeType: {projectInfo.ChangeType}, FieldName: {projectInfo.FieldName}");
                    }
                }
                finally
                {
                    dbAccess.CloseConnection();
                }
            }
        }
    }
}

